package com.open.capacity.reimburse.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.entity.PbdBudget;
import com.open.capacity.reimburse.excel.BudgetExcelModel;
import com.open.capacity.salary.entity.GzItem;
import com.open.capacity.salary.entity.GzRecordSalary;
import com.open.capacity.salary.entity.GzZtUser;
import com.open.capacity.salary.util.POIExcelUtil;
import com.open.capacity.user.model.SysDepartment;
import com.open.capacity.utils.BigDecimalUtil;
import com.open.capacity.utils.StringUtil;
import com.open.capacity.utils.SysUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.ImpKjpz;
import com.open.capacity.reimburse.dao.ImpKjpzDao;
import com.open.capacity.reimburse.service.ImpKjpzService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@Service
public class ImpKjpzServiceImpl implements ImpKjpzService {

    @Autowired
    private ImpKjpzDao impKjpzDao;

    /**
     * 添加
     *
     * @param impKjpz
     */
    public int save(ImpKjpz impKjpz) {
        return impKjpzDao.save(impKjpz);
    }

    /**
     * 修改
     *
     * @param impKjpz
     */
    public int update(ImpKjpz impKjpz) {
        return impKjpzDao.update(impKjpz);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return impKjpzDao.delete(id);
    }


    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<ImpKjpz> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<ImpKjpz> list = impKjpzDao.findAll(params);
        PageInfo<ImpKjpz> pageInfo = new PageInfo(list);

        return PageResult.<ImpKjpz>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<ImpKjpz> findAll(Map<String, Object> params) {
        return impKjpzDao.findAll(params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public long importExcel(MultipartFile multipartFile) throws Exception {
        InputStream inputStream = multipartFile.getInputStream();
        Workbook wb = WorkbookFactory.create(inputStream);

        Sheet sheet = wb.getSheetAt(0);
        // 获取最大行数(或者sheet.getLastRowNum())
        int rownum = sheet.getPhysicalNumberOfRows();
        // 获取第一行(表头)
        Row row = sheet.getRow(0);
        Assert.notNull(row, "数据格式不合法");
        Map<Integer, String> dyheader = new HashMap<>();
        dyheader.put(0, "pzrq");
        dyheader.put(1, "pzh");
        dyheader.put(2, "zy");
        dyheader.put(3, "j");
        dyheader.put(4, "d");
        dyheader.put(5, "pjlx");
        dyheader.put(6, "pjh");
        dyheader.put(7, "kjkm");
        dyheader.put(8, "dfkm");
        dyheader.put(9, "bmzcjjfl");
        dyheader.put(10, "czbkzl");
        dyheader.put(11, "czzb");
        dyheader.put(12, "ah");
        dyheader.put(13, "fyjjxz");
        dyheader.put(14, "msajzck");
        dyheader.put(15, "sayszk");
        dyheader.put(16, "wldx");
        dyheader.put(17, "xm");
        dyheader.put(18, "xszck");
        dyheader.put(19, "ysxm");
        dyheader.put(20, "zcgnflkm");
        dyheader.put(21, "zcfl");
        dyheader.put(22, "zffs");

        row = sheet.getRow(1);
        String companyName = POIExcelUtil.getString(row.getCell(0));
        row = sheet.getRow(2);
        String period = POIExcelUtil.getString(row.getCell(0));

        long count = 0;
        // 处理行数据
        for (int i = 4; i < rownum; i++) {
            row = sheet.getRow(i);
            // 遇到空行则结束
            if (row == null) {
                break;
            }
            if (row.getCell(0).getCellType().equals(CellType.BLANK)) {
                continue;
            }

            if(StringUtil.isEmpty(POIExcelUtil.getString(row.getCell(1)))) {
                continue;
            }

            Date pzrq = POIExcelUtil.getDate(row.getCell(0));
            String pzh = POIExcelUtil.getString(row.getCell(1));
            String zy = POIExcelUtil.getString(row.getCell(2));
            BigDecimal j = POIExcelUtil.getNumber(row.getCell(3));
            BigDecimal d = POIExcelUtil.getNumber(row.getCell(4));
            String pjlx = POIExcelUtil.getString(row.getCell(5));
            String pjh = POIExcelUtil.getString(row.getCell(6));
            String kjkm = POIExcelUtil.getString(row.getCell(7));
            String dfkm = POIExcelUtil.getString(row.getCell(8));
            String bmzcjjfl = POIExcelUtil.getString(row.getCell(9));
            String czbkzl = POIExcelUtil.getString(row.getCell(10));
            String czzb = POIExcelUtil.getString(row.getCell(11));
            String ah = POIExcelUtil.getString(row.getCell(12));
            String fyjjxz = POIExcelUtil.getString(row.getCell(13));
            String msajzck = POIExcelUtil.getString(row.getCell(14));
            String sayszk = POIExcelUtil.getString(row.getCell(15));
            String wldx = POIExcelUtil.getString(row.getCell(16));
            String xm = POIExcelUtil.getString(row.getCell(17));
            String xszck = POIExcelUtil.getString(row.getCell(18));
            String ysxm = POIExcelUtil.getString(row.getCell(19));
            String zcgnflkm = POIExcelUtil.getString(row.getCell(20));
            String zcfl = POIExcelUtil.getString(row.getCell(21));
            String zffs = POIExcelUtil.getString(row.getCell(22));

            ImpKjpz item = new ImpKjpz();
            item.setCompanyName(companyName);
            item.setPeriod(period);
            item.setPzrq(pzrq);
            item.setPzh(pzh);
            item.setZy(zy);
            item.setPjh(pjh);
            item.setAh(ah);
            item.setJ(j);
            item.setD(d);
            item.setKjkm(kjkm);
            item.setDfkm(dfkm);
            item.setBmzcjjfl(bmzcjjfl);
            item.setCzbkzl(czbkzl);
            item.setCzzb(czzb);
            item.setFyjjxz(fyjjxz);
            item.setSayszk(sayszk);
            item.setWldw(wldx);
            item.setXm(xm);
            item.setXszck(xszck);
            item.setMsajzck(msajzck);
            item.setZcgnflkm(zcgnflkm);
            item.setYsxm(ysxm);
            item.setZcfl(zcfl);
            item.setZffs(zffs);
            impKjpzDao.save(item);
            count++;
        }
        return count;
    }
}
